<template>
	<div class="bg">
		<div class="zzc" v-if="showzzc" @click="close"></div>
		<div class="container">
			<div class="title">任务分析</div>
			<div class="search_box">
				<div class="select_box" @click.stop="options_show=!options_show;showzzc=!showzzc">
					<div class="form-control" id="select" style="width: 120px;">
						<div :style="ind=='all'?'color:#999':''">
							{{ind!='all'?types[ind].name:'查询方式'}}
						</div>
						<img src="@assets/images/down.png" alt="">
					</div>
					<div class="options" v-if="options_show">
						<div class="option" @click.stop="selectit(index)" v-for="item,index in types">{{item.name}}
						</div>
					</div>
				</div>

				<el-date-picker v-if="ind!=2" prefix-icon='none' ref="year" style="width: 120px;margin-left: 10px;"
					v-model="year2" type="year" value-format="yyyy" placeholder="年份">
				</el-date-picker>
				<!-- 	<el-date-picker v-if="ind!=2" value-format="yyyy" v-model="year2" type="year" placeholder="选择年">
				</el-date-picker>
 -->
				<div class="select_box" v-if="ind==1" @click.stop="options_show3=!options_show3;showzzc=!showzzc">
					<div class="form-control" id="select" style="width: 120px;">
						<div :style="ind3=='all'?'color:#999;width:40px':'width:40px'">
							{{ind3!='all'?month2[ind3]:'月'}}
						</div>
						<div v-if="ind3!='all'" @click.stop="month='',ind3='all'">
							<img src="@assets/images/close.png" alt="">
						</div>

						<img src="@assets/images/down.png" alt="">
					</div>
					<div class="options" v-if="options_show3">
						<div class="option" @click.stop="selectit3(index)" v-for="item,index in month2">{{item}}
						</div>
					</div>
				</div>
				<el-date-picker v-if="ind==2" prefix-icon='none' style="width: 120px;margin-left: 10px;" v-model="date1"
					type="date" value-format="timestamp" placeholder="选择日期" @change="timeFormate(date1)">
				</el-date-picker>



				<div class="sea_btn sear_btns" @click="search">
					<img src="@assets/images/search.png" alt="">
					查询
				</div>

			</div>
			<div class="box">
				<div class="box_top">
					<div class="box_top_left">
						<!-- 	<div class="search">
							<el-date-picker prefix-icon='none' style="width: 120px;margin-left: 10px;" v-model="date1"
								type="date" value-format="timestamp" placeholder="选择日期" @change="timeFormate(date1)">
							</el-date-picker>
							<div class="sea_btn sear_btns" @click="getnum()">
								<img src="@assets/images/search.png" alt="">
								查询
							</div>
						</div> -->
						<div class="echart" ref="container"></div>
					</div>
					<div class="box_top_left">
						<!-- <div class="search">
							<el-date-picker prefix-icon='none' style="width: 120px;margin-left: 10px;" v-model="date2"
								type="date" value-format="timestamp" placeholder="选择日期" @change="timeFormate(date2)">
							</el-date-picker>
							<div class="sea_btn sear_btns" @click="gettime()">
								<img src="@assets/images/search.png" alt="">
								查询
							</div>
						</div> -->
						<div class="echart" ref="timelong"></div>
					</div>
				</div>
				<div class="table">
					<div class="table_top">
						<div class="table_title">按人员统计</div>
						<!-- <div class="top_btns">
								<div class="export" @click="export2Excel" v-if="$permissions.indexOf(85)  != -1">导出</div>
								<div class="print" @click="tabshow=true">打印</div>
							</div> -->
					</div>
					<el-table id="table" :data="tableData" style="width: 100%" :default-sort="{prop: 'code'}">
						<el-table-column prop="userName" label="人员" sortable>
						</el-table-column>
						<el-table-column prop="finish" label="已完成" sortable>
						</el-table-column>
						<el-table-column prop="unfinish" label="未完成" sortable>
						</el-table-column>
						<el-table-column prop="count" label="总任务数" sortable>
						</el-table-column>
						<el-table-column prop="finishPer" label="完成率" sortable>
						</el-table-column>
						<el-table-column prop="workTime" label="已完成工时" sortable>
						</el-table-column>
						<el-table-column prop="score" label="绩效评分" sortable>
						</el-table-column>
					</el-table>
			
				</div>
			</div>
		</div>
		<div class="loading-content" v-if="showloading">
			<div class="loading-spinner">
				<svg viewBox="25 25 50 50" class="circular">
					<circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
				</svg>
			</div>
			<div class="loading-text">加载中...</div>
		</div>
	</div>
</template>
<script>
	export default {
		name: "",
		data() {
			return {
				types: [{
						name: '按年份',
						id: 0
					},
					{
						name: '按月份',
						id: 1
					},
					{
						name: '按日期',
						id: 2
					},
				],
				month2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
				ind: 0,
				ind2: 'all',
				ind3: 'all',
				ax: [],
				ay: [],
				bx: [],
				by: [],
				tableData: [],
				date: "",
				year2: "",
				year: "",
				month: "",
				date1: "",
				date2: "",
				options_show: false,
				showzzc: false,
				options_show3: false,
				showloading: false
			}
		},
		mounted() {
			this.timeFormate();
			this.getnum();
			this.gettime();
			this.gettable();
		},

		methods: {
			gettable() {
				this.showloading = true
				var that = this
				var param = {
					year: this.year,
					month: this.month,
					day: this.date
				}
				this.$http.post(this.$url + '/rw/Statistic/byTable', this.$qs.stringify(param))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.tableData = response.data.data
						that.showloading = false
					})
					.catch(function(error) {
						console.log(error);
					});
			},
			// 点击遮罩层
			close() {
				this.options_show = false
				this.options_show3 = false
				this.showzzc = false
			},
			selectit(i) {
				this.ind = i
				this.options_show = false
				this.month = ''
				this.date = ''
				this.index3 = ''
				this.date1 = ''
				// this.year2 = new Date().getFullYear()

				this.showzzc = false
			},
			selectit3(i) {
				this.ind3 = i
				this.options_show3 = false
				this.month = this.month2[this.ind3]
				this.showzzc = false
			},
			search() {
				console.log(this.year2)
				if (!this.year2 && this.ind == 0) {
					this.$Toast('请选择年份');
					return
				}
				if (this.ind == 0) {
					this.month = ''
					this.year = this.year2
				}
				if (this.ind == 1) {
					if (!this.year2 || this.month == '' || this.year == null) {
						this.$Toast('请选择年份和月份');
						return
					}
					this.year = this.year2
					// return
				}
				if (this.ind == 2) {
					if (!this.date) {
						this.$Toast('请选择日期');
						return
					}
					// return
				}

				console.log(this.year)
				this.getnum()
				this.gettime()
				this.gettable()
			},
			//显示当前时间（年月日时分秒）
			timeFormate(timeStamp) {
				console.log(timeStamp)
				let year = ''
				let month = ''
				let date = ''
				if (timeStamp) {
					year = new Date(timeStamp).getFullYear();
					month = new Date(timeStamp).getMonth() + 1
					date = new Date(timeStamp).getDate()
					// return year + "-" + month + "-" + date;
					this.year = year;
					this.month = month;
					this.date = date;
				} else {
					year = new Date().getFullYear();
					month = new Date().getMonth() + 1
					date = new Date().getDate()
					this.date1 = new Date()
					this.date2 = new Date()
					this.year = year;
					// this.year2 = year;
					this.$forceUpdate()
				}
				
			},
			gettime() {
				this.showloading = true
				var that = this
				var param = {
					year: this.year,
					month: this.month,
					day: this.date
				}
				this.$http.post(this.$url + '/rw/Statistic/byWorkTime', this.$qs.stringify(param))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.bx = []
						that.by = []
						for (var i = 0; i < response.data.data.length; i++) {
							that.bx[i] = response.data.data[i].userName ? response.data.data[i].userName : ''
							that.by[i] = response.data.data[i].count
						}
						that.showloading = false
						that.gettimelong();
					})
					.catch(function(error) {
						console.log(error);
					});
			},
			getnum() {
				this.showloading = true
				var that = this
				var param = {
					year: this.year,
					month: this.month,
					day: this.date
				}
				this.$http.post(this.$url + '/rw/Statistic/byUser', this.$qs.stringify(param))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.ax = []
						that.ay = []
						for (var i = 0; i < response.data.data.length; i++) {
							that.ax[i] = response.data.data[i].userName ? response.data.data[i].userName : ''
							that.ay[i] = response.data.data[i].count
						}
						that.showloading = false
						that.getbar();
					})
					.catch(function(error) {
						console.log(error);
					});
			},

			gettimelong() {
				var dom = this.$refs.timelong
				var myChart = this.$echarts.init(dom, null, {
					renderer: 'canvas',
					useDirtyRect: false
				});
				var app = {};

				var option;

				option = {
					title: {
						text: "按工时统计",
						top: 0,
						textStyle: {
							fontSize: 16,
							color: "#626262",
						},
						// padding: 17,
						left: 14
					},
					grid: {
						top: 70,
						left: 20,
						right: 20,
						bottom: 9,
						containLabel: true
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'shadow'
						}
					},
					xAxis: [{
						type: 'category',
						data: this.bx,
						axisLabel: {
							interval: 0,
							rotate: 30
						}
					}],
					yAxis: [{
						type: 'value',
						name: '工时',
						min: 0,
						nameRotate: this.by.length == 0 ? 1 : 0,
						// max: 72,
						minInterval: 1,
						axisLabel: {
							formatter: '{value}小时'
						}

					}],
					series: [{
						name: "工时",
						barMaxWidth: 50,
						data: this.by,
						type: 'bar',
						itemStyle: {
							color: '#527cff',

						},
					}]
				};

				if (option && typeof option === 'object') {
					myChart.setOption(option);
				}

				window.addEventListener('resize', myChart.resize);
			},
			getbar() {
				var dom = this.$refs.container
				var myChart = this.$echarts.init(dom, null, {
					renderer: 'canvas',
					useDirtyRect: false
				});
				var app = {};

				var option;
				console.log(this.ax)
				option = {
					title: {
						text: "按任务数统计",
						top: 0,
						textStyle: {
							fontSize: 16,
							color: "#626262",
						},
						// padding: 17,
						left: 14
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'shadow'
						}
					},
					legend: {
						right: 20,
						top: 34
					},
					grid: {
						top: 70,
						left: 20,
						right: 20,
						bottom: 9,
						containLabel: true
					},
					xAxis: [{
						type: 'category',
						data: this.ax,
						axisLabel: {
							interval: 0,
							rotate: 30
						}
					}],
					yAxis: [{
						type: 'value',
						name: '任务数量',
						min: 0,
						nameRotate: this.ay.length == 0 ? 1 : 0,
						// max: 5,
						minInterval: 1,

					}],
					series: [{
						name: '总数',
						barMaxWidth: 50,
						type: 'bar',
						stack: 'Ad',
						emphasis: {
							focus: 'series'
						},
						itemStyle: {
							color: '#527cff'
						},
						data: this.ay
					}, ]
				};

				if (option && typeof option === 'object') {
					myChart.setOption(option);
				}

				window.addEventListener('resize', myChart.resize);
			},

		}
	}
</script>
<style scoped>
	.search {
		display: flex;
		margin-bottom: 30px;
	}

	.title {
		font-size: 24px;
		color: #000000;
	}

	/deep/ .el-input__icon {
		line-height: 34px;
	}

	/deep/ .el-table .el-table__cell {
		text-align: center;
	}

	/deep/ .el-table .el-table__cell:nth-of-type(1) {
		text-align: left;
	}

	/deep/ .el-table .el-table__cell:nth-of-type(2) {
		text-align: left;
	}

	.table_top {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 23px;
		margin-bottom: 11px;
	}

	.table_title {
		font-size: 16px;
		font-weight: bold;
		color: #626262;
	}

	.top_btns {
		display: flex;
	}

	.top_btns div {
		font-size: 14px;
		color: #FFFFFF;
		text-align: center;
		line-height: 28px;
		border-radius: 3px;
		width: 63px;
		height: 28px;
		margin-left: 13px;
		cursor: pointer;
	}

	.print {
		background: #32B16C
	}

	.export {
		background: #5BC0DE;
	}

	.bg {
		background: #f6f6f6;
		min-height: 100vh;
	}

	.box_top_left {
		padding-top: 17px;

		width: 49%;
		background-color: #fff;
	}

	.echart {
		width: 100%;
		height: 370px;
	}

	.box_top {
		display: flex;
		justify-content: space-between;
		margin-top: 17px;
	}

	.box {
		-ms-overflow-style: none !important;
	}

	.box::-webkit-scrollbar {
		display: none;
	}

	.box {
		width: 100%;

		overflow-y: scroll;
		height: calc(100vh - 116px);
	}

	/deep/ .el-input--prefix .el-input__inner {
		height: 34px;
		padding: 0 8px;
	}

	.input-cont:hover .clears {
		display: block;
	}

	.clears {
		width: 10px;
		position: absolute;
		right: 35px;
		top: 14px;
		display: none;
		cursor: pointer;
	}

	.el-input__inner {
		width: 114px;
		height: 34px;
		background: #F6F6F6;
		border: 1px solid #CCCCCC;
		box-shadow: 0px 2px 0px 0px #EEEEEE;
		border-radius: 5px;
	}
</style>
